package com.qq.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qq.mall.beans.Admin;
import com.qq.mall.mapper.AdminMapper;
import com.qq.mall.service.AdminService;
import com.qq.mall.utils.MD5Utils;
import com.qq.mall.vo.ResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
    @Resource
    AdminMapper adminMapper;

    @Override
    public ResultVO adminResgit(String name, String pwd) {
        synchronized (this) {
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("adminname", name);
            Admin admin = adminMapper.selectOne(adminQueryWrapper);
            if (admin == null) {
                String md5Pwd = MD5Utils.md5(pwd);
                Admin admin1 = new Admin();
                admin1.setAdminname(name);
                admin1.setAdminpwd(md5Pwd);
                int i = adminMapper.insert(admin1);
                if (i > 0) {
                    return new ResultVO(10000, "注册成功！", admin1);
                } else {
                    return new ResultVO(10001, "注册失败！", null);
                }
            } else {
                return new ResultVO(10001, "用户名已经被注册！", null);
            }
        }
    }

    @Override
    public ResultVO checkLogin(String name, String pwd) {
        synchronized (this) {
            QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("adminname", name);
            Admin admin = adminMapper.selectOne(adminQueryWrapper);
            if (admin == null) {
                return new ResultVO(10001, "用户不存在", null);
            } else {
                String md5Pwd = MD5Utils.md5(pwd);
                assert md5Pwd != null;
                if (md5Pwd.equals(admin.getAdminpwd())) {
                    //如果登录验证成功，则需要生成令牌token（token就是按照特定规则生成的字符串）
                    //使用jwt规则生成token字符串
                    JwtBuilder builder = Jwts.builder();

                    HashMap<String, Object> map = new HashMap<>();
                    map.put("key1", "value1");
                    map.put("key2", "value2");

                    String token = builder.setSubject(name)                     //主题，就是token中携带的数据
                            .setIssuedAt(new Date())                            //设置token的生成时间
                            .setId(admin.getAdminId() + "")               //设置用户id为token  id
                            .setClaims(map)                                     //map中可以存放用户的角色权限信息
                            .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)) //设置token过期时间
                            .signWith(SignatureAlgorithm.HS256, "lzp1")     //设置加密方式和加密密码
                            .compact();

                    return new ResultVO(10000, token, admin);
                } else {
                    return new ResultVO(10001, "登录失败，密码错误！", null);
                }
            }
        }

    }

    @Override
    public Admin findByName(String name) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("adminname", name);
        return adminMapper.selectOne(adminQueryWrapper);
    }
}
